package com.kuwei.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.kuwei.utils.Constants;

@Service("procedureService")
public class ProcedureService extends CommonService {
	
	public List<Map<String, Object>> queryProcedure(Object map) throws Exception{
		try{
			String service_code ="PROCEDURE.QUERY_PROCEDURE";
			return commonQry(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	
	public List<Map<String, Object>> queryUserProcedure(Object map) throws Exception{
		try{
			String service_code ="PROCEDURE.QUERY_USER_PROCEDURE";
			return commonQry(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	
	public List<Map<String, Object>> queryProcedureNum(Object map) throws Exception{
		try{
			String service_code ="PROCEDURE_NUM.QUERY_PROCEDURE_NUM";
			return commonQry(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	
	public int insertProcedure(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_NAME", map.get("PRO_NAME"));
			List<Map<String, Object>> list = queryProcedure(reqMap);
			if(list.size()>0){
				return -1;
			} else{
				String service_code ="PROCEDURE.INSERT_PROCEDURE";
				return commonInsert(service_code,map);
			}
			
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	
	/**
	 * 当user_id pro_id prod_id对应的工序记录存在时，则更新数据，否则新增
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public int insertUserProcedure(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_ID", map.get("PRO_ID"));
			reqMap.put("PROD_ID", map.get("PROD_ID"));
			reqMap.put("USER_ID", map.get("USER_ID"));
			List<Map<String, Object>> list = queryProcedureNum(reqMap);
			if(list.size()>0) {
//				String service_code ="PROCEDURE_NUM.UPDATE_PROCEDURE_NUM";
//				return commonUpdate(service_code,map);
				return -2;
			}
			else{
				String service_code ="PROCEDURE_NUM.INSER_PROCEDURE_NUM";
				return commonInsert(service_code,map);
			}
			
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	public int updateProcedureNum(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_ID", map.get("PRO_ID"));
			reqMap.put("PROD_ID", map.get("PROD_ID"));
			reqMap.put("USER_ID", map.get("USER_ID"));
			List<Map<String, Object>> list = queryProcedureNum(reqMap);
			if(list.size()>0) {
				String service_code ="PROCEDURE_NUM.UPDATE_PROCEDURE_NUM";
				return commonUpdate(service_code,map);
			}
			else{
				return -1;
			}
		}catch(Exception e){
			throw new Exception(this.getClass().toString()+e,e);
		}
	}
	
	public int deleteProcedureNum(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_ID", map.get("PRO_ID"));
			reqMap.put("PROD_ID", map.get("PROD_ID"));
			reqMap.put("USER_ID", map.get("USER_ID"));
			reqMap.put("STATUS_CD", Constants.STATE_9999);
			List<Map<String, Object>> list = queryProcedureNum(reqMap);
			if(list.size()>0) {
				return -1;
			}
			else{
				String service_code ="PROCEDURE_NUM.UPDATE_PROCEDURE_NUM";
				return commonUpdate(service_code,map);
			}
		}catch(Exception e){
			throw new Exception(this.getClass().toString()+e,e);
		}
	}
	public int modifyProcedureNum(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_ID", map.get("PRO_ID"));
			reqMap.put("PROD_ID", map.get("PROD_ID"));
			reqMap.put("USER_ID", map.get("USER_ID"));
			reqMap.put("STATUS_CD", Constants.STATE_9999);
			List<Map<String, Object>> list = queryProcedureNum(reqMap);
			if(list.size()>0) {
				return -1;
			}
			else{
				String service_code ="PROCEDURE_NUM.UPDATE_PROCEDURE_NUM";
				return commonUpdate(service_code,map);
			}
		}catch(Exception e){
			throw new Exception(this.getClass().toString()+e,e);
		}
	}
	
	public int passProcedureNum(Map<String, Object> map) throws Exception {
		try{
			Map<String, Object> reqMap = new HashMap<String, Object>();
			reqMap.put("PRO_ID", map.get("PRO_ID"));
			reqMap.put("PROD_ID", map.get("PROD_ID"));
			reqMap.put("USER_ID", map.get("USER_ID"));
			reqMap.put("STATUS_CD", Constants.STATE_1000);
			List<Map<String, Object>> list = queryProcedureNum(reqMap);
			if(list.size()>0) {
				return -1;
			}
			else{
				String service_code ="PROCEDURE_NUM.UPDATE_PROCEDURE_NUM";
				return commonUpdate(service_code,map);
			}
		}catch(Exception e){
			throw new Exception(this.getClass().toString()+e,e);
		}
	}
	
	public int deleteTest(Map<String, Object> map) throws Exception {
		try{
			String service_code ="TEST.DELETETEST";
			return commonDelete(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
}
